package com.luo.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.luo.constant.MessageConstant;
import com.luo.entity.PageResult;
import com.luo.entity.QueryPageBean;
import com.luo.entity.Result;
import com.luo.pojo.Address;
import com.luo.service.AddressService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author Zoro
 * @date 2021年09月02
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Reference
    private AddressService addressService;


    @RequestMapping("/findAllMaps.do")
    public List<Address> findAllMaps() {
        return addressService.findAllMaps();
    }


    /**
     * 分页查询数据
     */
    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        PageInfo<Address> addressPageInfo = addressService.findPage(queryPageBean);
        return new PageResult(addressPageInfo.getTotal(), addressPageInfo.getList());
    }


    /**
     * 添加数据
     */
    @RequestMapping("/addAddress.do")
    public Result addAddress(@RequestBody Address address) {

        try {
            addressService.addAddress(address);
            return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ADD_ADDRESS_FAIL);
        }
    }

    @RequestMapping("/deleteById.do")
    public Result deleteById(Integer id) {
        try {
            addressService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.DELETE_ADDRESS_FAIL);
        }
    }


    @RequestMapping("/getAddressById.do")
    public Map<String, Object> getAddressById(Integer id) {
        return addressService.getAddressById(id);
    }
}
